﻿using UnityEngine;
using XFABManager;

namespace XFGameFramework.Localization
{
    /// <summary>
    /// 图片类型本地化组件
    /// </summary>
    [RequireComponent(typeof(ImageLoader))]
    [HelpURL("https://gitee.com/xianfengkeji/xfgame-framework.localization/blob/master/Documentation~/ClassApi/LocalizeImage.md")]
    public class LocalizeImage : LocalizeBase
    {

        private ImageLoader loader;


        private void Awake()
        {
            loader = GetComponent<ImageLoader>();
        }


        protected override void OnLanguageValueChange()
        {
            base.OnLanguageValueChange();

            LanguageInfo info = LocalizationManager.GetLanguageInfo(ID);
            SetValue(info);

        }

        
        private void SetValue(LanguageInfo info) 
        {

#if UNITY_EDITOR
            if (!Application.isPlaying && loader == null) 
            {
                loader = GetComponent<ImageLoader>();
            }

#endif

            if (loader == null)  
                return; 

            switch (info.type)
            {
                case MultiLanguageType.Image:
                    loader.Type = ImageLoaderType.AssetBundle;
                    loader.ProjectName = info.image.projectName;
                    loader.AssetName = info.image.imageName;
                    break;

                case MultiLanguageType.NetworkImage:
                    loader.Type = ImageLoaderType.Network;
                    loader.Path = info.url; 
                    break;
            }

        }



    }
    
}

